package com.xtaller.sdboot.utils.helper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author : Taller
 * @date : 2020/2/7 08:09
 * @Description : 文件操作
 */
public class FileHelper {
    /**
     * 写文件
     * @param
     * 		path 	文件存储路径
     * 		sb		文件内容
     * 		isNew	文件存在是否删除新建 true是 false否
     * */
    public static void write(String path, String fileName, StringBuffer sb, Boolean isNew) {
        File file = new File(path);
        //如果文件夹不存在
        if(!file.exists()){
            file.mkdir();
        }
        file = new File(path + "/" + fileName);
        if(isNew) {
            if(file.exists()){
                file.delete();
            }
            opt(file, sb);
        }else {
            if(!file.exists()){
                opt(file, sb);
            }
        }
    }
    private static void opt(File file, StringBuffer sb) {
        FileWriter fw = null;
        BufferedWriter writer = null;
        try {
            file.createNewFile();
            fw = new FileWriter(file);
            writer = new BufferedWriter(fw);
            writer.write(sb.toString());

            writer.flush();
            writer.close();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
